#!/usr/bin/python

#get subdatabase for selected ligands

import sys, string, os

#Name of database directory
database_dir = sys.argv[1]

#Name of list with labels
list = sys.argv[2]

#Name of new database
new_db = sys.argv[3]
new_db_file = open(new_db, 'w')

compare_list = []


#---------------------------------------
#Convert labels into list
label_list = []
list_file = open(list, 'r')
for i in list_file.xreadlines():
  label_list.append(string.strip(i))
list_file.close()


#read names of directory files
dir_list = os.listdir(database_dir)

label = ''


#for i in dir_list:
for file in dir_list:
  print file
  if file[-2:] == 'db':
   database_file = open(file, 'r')
   print "processing file " + file
   family = 'false'
   molecule = []
   found = 'false'
   for i in database_file.xreadlines():
     #print i[0:6]
     if family == 'true':
      #print 'here'
      label = string.strip(i[47:57])
      #print label
      family = 'false'
      if label in label_list:
        found = 'true'
	compare_list.append(label)
	#print label + ' found'
	molecule.append(i)
     elif found == 'true' and i[0:6] <> 'Family':
	molecule.append(i)
     if i[0:6] =='Family':
       family = 'true'
       #print label, found, '--------------------------'	
       if found == 'true':
         #print 'save molecule'
	 for k in molecule:
	   print k
	   new_db_file.write(k)
	 found = 'false'
	 #print '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
       molecule = []
       molecule.append(i)
	
     
#----------------------
#clean up
database_file.close()
new_db_file.close()
  






